package cn.edu.nit.sys.service.impl;

import cn.edu.nit.sys.entity.Approve;
import cn.edu.nit.sys.entity.Leave;
import cn.edu.nit.sys.mapper.LeaveMapper;
import cn.edu.nit.sys.service.LeaveService;
import cn.edu.nit.sys.utils.R;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

/**
 * @Author xiaobai
 * @Date 2023/3/19 20:40
 * @Version 1.0.0
 */
@Service
public class LeaveServiceImpl extends ServiceImpl<LeaveMapper, Leave> implements LeaveService {

    @Override
    public R addLeave(Leave leave) {
        if(leave.getDay() <= 3) {
            leave.setLeaveType("0");
        }else if(leave.getDay() <= 7) {
            leave.setLeaveType("1");
        }else {
            leave.setLeaveType("2");
        }
        String id = new Date().getTime() + "";
        Approve approve = new Approve();
        approve.setFlowId(id);
        leave.setId(id);
        leave.setFlow("1");
        int rs = baseMapper.insert(leave);
        rs += baseMapper.insertApprove(UUID.randomUUID().toString().replaceAll("-", ""), id);
        return rs > 0 ? R.ok("提交成功，等待审批") : R.error("提交失败，请稍后重试");
    }

    @Override
    public R getLeaveList(Page<Leave> page, String uid) {
        Page<Leave> list = baseMapper.getLeaveList(page, uid);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("total", list.getTotal());
        jsonObject.put("list", list.getRecords());
        return R.ok(jsonObject);
    }

    @Override
    public R getDetail(String id) {
        Leave detail = baseMapper.getDetail(id);
        return R.ok(detail);
    }
}
